package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.ReservationType;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author system
 * @since 2024-12-20 22:31:05
 */
@Component
public class ReservationTypeSpecifications {

    public static Specification<ReservationType> queryList(ReservationType reservationType) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (reservationType.getId() != null) {
                predicates.add(builder.equal(root.get("id" ).as(Integer.class), reservationType.getId()));
            }
            if (reservationType.getType() != null) {
                predicates.add(builder.like(root.get("type" ).as(String.class), "%" + reservationType.getType() + "%" ));
            }
            if (reservationType.getNotice() != null) {
                predicates.add(builder.like(root.get("notice" ).as(String.class), "%" + reservationType.getNotice() + "%" ));
            }
            if (reservationType.getDetails() != null) {
                predicates.add(builder.like(root.get("details" ).as(String.class), "%" + reservationType.getDetails() + "%" ));
            }
            if (reservationType.getOthers() != null) {
                predicates.add(builder.like(root.get("others" ).as(String.class), "%" + reservationType.getOthers() + "%" ));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
